package com.gjy.util;

import java.lang.reflect.Field;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-01-18 14:26:36
 */
public class ReflectUtil {

    private static final String NAME = "ReflectUtil";

    public static <T> void set(T t, String fieldName, Object value) {
        try {
            Class<?> clazz = t.getClass();
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            field.set(t, value);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> Object get(T t, String fieldName) {
        try {
            Class<?> clazz = t.getClass();
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            return field.get(t);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
